package com.powernode.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.entity.SysLog;
import com.powernode.model.Result;
import com.powernode.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统操作日志管理控制层
 */
@Api(tags = "系统操作日志接口管理")
@RequestMapping("sys/log")
@RestController
public class SysLogController {

    @Resource
    private SysLogService sysLogService;

    @ApiOperation("多条件分页查询系统操作日志")
    @GetMapping("page")
    @PreAuthorize("hasAnyAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation){

        //1.创建分页对象
        Page<SysLog> page = new Page<>();
        //2.分页查询
        sysLogService.page(page,new LambdaQueryWrapper<SysLog>()
                //如果查询条件eq和like都存在，优先eq，但是结果是一样的
                .eq(ObjectUtil.isNotEmpty(userId),SysLog::getUserId,userId)
                .like(StrUtil.isNotBlank(operation),SysLog::getOperation,operation)
        );

        return Result.success(page);
    }

}
